package ro.upt.pcbe;

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Topic;

/**
 *
 * @author Roxi
 */
public class EditorialOffice {

    private LinkedList<Editor> editors;
    
    @Resource(mappedName = "MyFactory")
    private static ConnectionFactory connectionFactory;
    
    @Resource(mappedName = "Science")
    private static Topic science;
    
    @Resource(mappedName = "Sport")
    private static Topic sport;
    
    @Resource(mappedName = "ConfirmationTopic")
    private static Topic confirmationTopic;
    
    @Resource(mappedName = "Science.Physics")
    private static Topic sciencephysics;

    public EditorialOffice() {
        this.editors = new LinkedList<Editor>();
    }

    public void addEditor(Editor editor) {
        editors.add(editor);
    }

    public LinkedList<Editor> getEditors() {
        return editors;
    }

    public static void main(String[] args) {
        EditorialOffice office = new EditorialOffice();
        Connection connection = null;
        InputStreamReader inputStreamReader;
        char answer = '\0';
        try {
            connection = connectionFactory.createConnection();

            Editor ed = new Editor("bubu", connection, office);

            ed.subscribeToConfirmation(confirmationTopic);

            ed.publish("Random Title", science, "Breaking News", "Just me writing smth");

            ed.publish("Sport Title", sport, "BBC", "Sport");

            ed.delete("Random Title", science);
            
            ed.update("Sport Title", "Just me writing an update!!", sport);

            Editor ed2 = new Editor("mumu", connection, office);
            
            ed2.subscribeToConfirmation(confirmationTopic);
            
            ed2.publish("Random Title 2", sport, "Sport news", "bla bla");
            
            ed2.publish("Physics", sciencephysics, "Einstein's new theory", "E=m*c^2");

            connection.start();

            System.out.println("To end program, type Q or q, " + "then <return>");
            inputStreamReader = new InputStreamReader(System.in);

            while (!((answer == 'q') || (answer == 'Q'))) {
                try {
                    answer = (char) inputStreamReader.read();
                } catch (IOException e) {
                    System.err.println("I/O exception: " + e.toString());
                }
            }

        } catch (JMSException e) {
            System.err.println("Exception occurred: " + e.toString());
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (JMSException e) {
                }
            }
        }
    }
}
